package com.game.league.articles.panel;

import android.app.Activity;

import androidx.recyclerview.widget.GridLayoutManager;

import com.game.base.app.base.BasePanel;
import com.game.league.R;
import com.game.league.articles.adapter.ArticlesAdapter;
import com.game.league.articles.table.Articles;
import com.game.league.databinding.ArticlesPanelBinding;
import com.game.league.service.InterfaceMgr;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * 物品列表展示
 * Created by Su on 2019/5/30.
 */
public class ArticlesPanel extends BasePanel<ArticlesPanelBinding> {
    private ArrayList<Articles> list;
    private ArticlesAdapter adapter;
    private int type;   // 物品类型

    @Override
    protected int getLayoutId() {
        return R.layout.articles_panel;
    }

    public ArticlesPanel(Activity activity, int type) {
        super(activity);
        this.type = type;
        initView();
    }

    private void initView() {
        binding.customFrameLayout.show(R.id.emptyView);
        binding.emptyView.show(true);
        binding.recyclerList.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        adapter = new ArticlesAdapter(getActivity());
        binding.recyclerList.setAdapter(adapter);

        InterfaceMgr.getInstance().getArticlesList(type, new FindListener<Articles>() {
            @Override
            public void done(List<Articles> list, BmobException e) {
                refreshArticlesView((ArrayList<Articles>) list, e);
            }
        });
    }

    private void refreshArticlesView(ArrayList<Articles> list, BmobException e) {
        this.list = list;
        if (e != null || list == null || list.isEmpty()) {
            showNoData();
            return;
        }
        showData();
    }

    private void showNoData() {
        binding.customFrameLayout.show(R.id.emptyView);
        binding.emptyView.show("暂无数据", null);
    }

    private void showData() {
        binding.customFrameLayout.show(R.id.recycler_list);
        adapter.setItems(list);
    }
}
